<template>
  <div class="app-container">
    <el-form class="formList" :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-row>
        <el-form-item label="投诉类型" prop="complaintType">
          <el-select v-model="queryParams.complaintType" placeholder="请选择投诉类型" clearable size="small">
            <el-option v-for="dict in complaintTypeOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue" />
          </el-select>
        </el-form-item>
        <el-form-item label="投诉标题" prop="complaintTitle">
          <el-input v-model="queryParams.complaintTitle" placeholder="请输入投诉标题" clearable size="small"
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="投诉时间" prop="createDateArray">
          <el-date-picker v-model="queryParams.createDateArray" type="daterange" align="right" clearable
            value-format="yyyy-MM-dd" unlink-panels range-separator="至" start-placeholder="开始日期"
            end-placeholder="结束日期" :picker-options="pickerOptions">
          </el-date-picker>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="订单编号" prop="orderNo">
          <el-input v-model="queryParams.orderNo" placeholder="请输入订单编号" clearable size="small"
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="客户名称" prop="customerName">
          <el-input v-model="queryParams.customerName" placeholder="请输入客户名称" clearable size="small"
            @keyup.enter.native="handleQuery" />
        </el-form-item>
      <!-- <el-col :span="8">
        <el-form-item label="处理状态" prop="processStatus">
          <el-select v-model="queryParams.processStatus" placeholder="请选择处理状态" clearable size="small">
            <el-option v-for="dict in processStatusOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue" />
          </el-select>
        </el-form-item>
      </el-col> -->
        <el-form-item label="处理时间" prop="processDateArray">
          <el-date-picker v-model="queryParams.processDateArray" type="daterange" align="right" clearable
            value-format="yyyy-MM-dd" unlink-panels range-separator="至" start-placeholder="开始日期"
            end-placeholder="结束日期" :picker-options="pickerOptions">
          </el-date-picker>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="完成时间" prop="finishDateArray">
          <el-date-picker v-model="queryParams.finishDateArray" type="daterange" align="right" clearable
            value-format="yyyy-MM-dd" unlink-panels range-separator="至" start-placeholder="开始日期"
            end-placeholder="结束日期" :picker-options="pickerOptions">
          </el-date-picker>
        </el-form-item>
        <el-form-item >
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-row>
    </el-form>

    <!-- 标签 -->
    <el-row :gutter="12" class="row-mbinfo">
      <el-col :span="6">
        <el-button-group>
          <el-button :class="activeClass == 0 ? 'button-sku-blue':''" @click="handleQueryStatus(0)" size="mini">全部
          </el-button>
          <el-button :class="activeClass == 1 ? 'button-sku-blue':''" @click="handleQueryStatus(1)" size="mini">待处理
          </el-button>
          <el-button :class="activeClass == 2 ? 'button-sku-blue':''" @click="handleQueryStatus(2)" size="mini">处理中
          </el-button>
          <el-button :class="activeClass == 3 ? 'button-sku-blue':''" @click="handleQueryStatus(3)" size="mini">已处理
          </el-button>
        </el-button-group>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['order:complaint:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['order:complaint:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['order:complaint:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['order:complaint:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->
    <div class="divTable">
      <el-table v-loading="loading" :data="complaintList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <!-- <el-table-column label="投诉ID" align="center" prop="complaintId" sortable /> -->
        <el-table-column label="订单编号" align="center" prop="orderNo" :show-overflow-tooltip="true" width="140">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="handleShowDetail(scope.row)">{{ scope.row.orderNo }}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column label="投诉类型" align="center" prop="complaintType" :formatter="complaintTypeFormat" />
        <el-table-column label="投诉图片" align="center" prop="complaintImageOne" :show-overflow-tooltip="true" >
          <template slot-scope="scope">
            <el-image v-if="scope.row.complaintImageOne != null && scope.row.complaintImageOne != '' " slot="reference"
            :src="scope.row.complaintImageOne" class="image-rows" :preview-src-list="[scope.row.complaintImageOne]">
          </el-image>
          </template>
        </el-table-column>
        <el-table-column label="投诉标题" align="center" prop="complaintTitle" :show-overflow-tooltip="true" />
        <el-table-column label="具体内容" align="center" prop="complaintContent" :show-overflow-tooltip="true" />
        <!-- <el-table-column label="客户ID" align="center" prop="customerUid" :show-overflow-tooltip="true" /> -->
        <el-table-column label="客户名称" align="center" prop="customerName" :show-overflow-tooltip="true" />
        <el-table-column label="投诉时间" align="center" prop="createDate" width="180" sortable>
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <!-- <el-table-column label="数据状态" align="center" prop="dataStatus" :formatter="dataStatusFormat" /> -->
        <el-table-column v-if="activeClass == 0" label="处理状态" align="center" prop="processStatus" :formatter="processStatusFormat" />
        <el-table-column v-if="activeClass != 1" label="处理人" align="center" prop="processUserName" :show-overflow-tooltip="true" />
        <el-table-column v-if="activeClass != 1" label="处理时间" align="center" prop="processDate" width="180" sortable>
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.processDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="activeClass != 1 && activeClass != 2 " label="处理完成时间" align="center" prop="finishDate" width="180" sortable>
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.finishDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="activeClass != 1" label="处理意见" align="center" prop="processRemark" :show-overflow-tooltip="true" />
        <el-table-column label="更新时间" align="center" prop="updateDate" width="180" sortable>
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.updateDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <!-- <el-table-column label="修改人名称" align="center" prop="updateName" :show-overflow-tooltip="true" /> -->
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <!-- <el-button size="mini" type="text" icon="el-icon-zoom-in" @click="handleShowDetail(scope.row)"
              v-hasPermi="['order:complaint:query']">查看</el-button> -->
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
              v-hasPermi="['order:complaint:edit']" v-if="scope.row.processStatus == '0'" >受理</el-button>
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleAudit(scope.row)"
              v-hasPermi="['order:complaint:edit']" v-if="scope.row.processStatus == '1'" >审核</el-button>
            <!-- <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
              v-hasPermi="['order:complaint:remove']">删除</el-button> -->
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>

    <!-- 添加或修改客户投诉对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1400px" append-to-body @keyup.enter.native="submitForm">
      <!-- 订单详情 -->
      <order v-if="orderVo" :info="orderVo" ></order>

      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="处理意见" prop="processRemark">
          <el-input v-model="form.processRemark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm(true)">完 成</el-button>
        <el-button type="primary" @click="submitForm(false)">失 败</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看客户投诉对话框 -->
    <el-dialog :title="viewTitle" :visible.sync="viewOpen" width="1400px" append-to-body>
      <order v-if="orderVo" :info="orderVo" ></order>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listComplaint,
    getComplaint,
    delComplaint,
    addComplaint,
    updateComplaint,
    updateStatus
  } from "@/api/order/complaint";
  import ImageUpload from '@/components/ImageUpload';
  import Editor from '@/components/Editor';

  import order from "@/components/Order/order";

  export default {
    name: "Complaint",
    components: {
      ImageUpload,
      Editor,
      order,
    },
    data() {
      return {
        pickerOptions: {
          shortcuts: [{
            text: '最近一周',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
              picker.$emit('pick', [start, end])
            }
          }]
        },
        // 默认数字
        activeClass: 0,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 客户投诉表格数据
        complaintList: [],
        // 弹出层标题
        title: "",
        //查看详细标题
        viewTitle: "",
        // 是否显示弹出层
        open: false,
        //是否显示查看详细弹出层
        viewOpen: false,
        //查看详细数据
        showData: [],
        // 投诉类型字典
        complaintTypeOptions: [],
        // 数据状态字典
        dataStatusOptions: [],
        // 处理状态字典
        processStatusOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          orderNo: null,
          complaintType: null,
          complaintTitle: null,
          customerName: null,
          createDate: null,
          dataStatus: null,
          processStatus: null,
          processDate: null,
          finishDate: null,
          createDateArray: null,
          processDateArray: null,
          finishDateArray: null,
        },
        columns: [{
            label: '投诉ID',
            dataIndex: 'complaintId',
          },
          {
            label: '订单ID',
            dataIndex: 'orderUid',
          },
          {
            label: '订单编号',
            dataIndex: 'orderNo',
          },
          {
            label: '投诉类型',
            dataIndex: 'complaintType',
          },
          {
            label: '投诉标题',
            dataIndex: 'complaintTitle',
          },
          {
            label: '具体内容',
            dataIndex: 'complaintContent',
          },
          {
            label: '投诉图片',
            dataIndex: 'complaintImage',
          },
          {
            label: '客户ID',
            dataIndex: 'customerUid',
          },
          {
            label: '客户名称',
            dataIndex: 'customerName',
          },
          {
            label: '投诉时间',
            dataIndex: 'createDate',
          },
          {
            label: '数据状态',
            dataIndex: 'dataStatus',
          },
          {
            label: '处理状态',
            dataIndex: 'processStatus',
          },
          {
            label: '处理人',
            dataIndex: 'processUser',
          },
          {
            label: '处理人名称',
            dataIndex: 'processUserName',
          },
          {
            label: '处理时间',
            dataIndex: 'processDate',
          },
          {
            label: '处理完成时间',
            dataIndex: 'finishDate',
          },
          {
            label: '处理意见',
            dataIndex: 'processRemark',
          },
          {
            label: '修改人',
            dataIndex: 'updateUser',
          },
          {
            label: '修改时间',
            dataIndex: 'updateDate',
          },
          {
            label: '修改人名称',
            dataIndex: 'updateName',
          },
        ],
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          processRemark: [{
            required: true,
            message: "处理意见不能为空",
            trigger: "blur"
          }],
        },
        orderVo:{}
      };
    },
    created() {
      this.getList();
      this.getDicts("order_complaint_type").then(response => {
        this.complaintTypeOptions = response.data;
      });
      this.getDicts("sys_data_status").then(response => {
        this.dataStatusOptions = response.data;
      });
      this.getDicts("order_process_status").then(response => {
        this.processStatusOptions = response.data;
      });
    },
    methods: {
      /** 点击按钮进行刷新 */
      handleQueryStatus(index) {
        this.activeClass = index;
        if (index == 0) {
          this.queryParams.processStatus = null;
        } else if (index == 1) {//待处理
          this.queryParams.processStatus = "0";
        } else if (index == 2) {//处理中
          this.queryParams.processStatus = "1";
        } else if (index == 3) {//已处理
          this.queryParams.processStatus = "2";
        }
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 查询客户投诉列表 */
      getList() {
        this.loading = true;
        listComplaint(this.queryParams).then(response => {
          this.complaintList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 投诉类型字典翻译
      complaintTypeFormat(row, column) {
        return this.selectDictLabel(this.complaintTypeOptions, row.complaintType);
      },
      // 数据状态字典翻译
      dataStatusFormat(row, column) {
        return this.selectDictLabel(this.dataStatusOptions, row.dataStatus);
      },
      // 处理状态字典翻译
      processStatusFormat(row, column) {
        return this.selectDictLabel(this.processStatusOptions, row.processStatus);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.orderVo = null;
        this.form = {
          complaintId: null,
          orderUid: null,
          orderNo: null,
          complaintType: null,
          complaintTitle: null,
          complaintContent: null,
          complaintImage: null,
          customerUid: null,
          customerName: null,
          createDate: null,
          dataStatus: null,
          processStatus: null,
          processUser: null,
          processUserName: null,
          processDate: null,
          finishDate: null,
          processRemark: null,
          updateUser: null,
          updateDate: null,
          updateName: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.activeClass = 0;
        this.queryParams.processDateArray = null;
        this.queryParams.finishDateArray = null;
        this.queryParams.createDateArray = null;
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.complaintId)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加客户投诉";
      },
      /** 查看详情按钮操作 **/
      handleShowDetail(row) {
        this.orderVo = null;
        const complaintId = row.complaintId;
        getComplaint(complaintId).then(response => {
          this.orderVo = response.data;
          this.viewOpen = true;
          this.viewTitle = "订单详情";
        });
      },
      handCloseDetail() {
        this.viewOpen = false
        this.showData = []
      },
      /** 受理按钮操作 */
      handleUpdate(row) {
        const processStatus = '1';
        const complaintId = row.complaintId || this.ids
        updateStatus(complaintId,processStatus).then(response => {
          this.msgSuccess("受理成功");
          this.getList();
        });
        // this.reset();
        // getComplaint(complaintId).then(response => {
        //   this.form = response.data;
        //   this.open = true;
        //   this.title = "修改客户投诉";
        // });
      },
      /**
       * 审核
       */
      handleAudit(row){
        this.reset();
        const complaintId = row.complaintId;
        getComplaint(complaintId).then(response => {
          this.orderVo = response.data;
          if(response.data.complaintList && response.data.complaintList.length > 0){
            this.form.complaintId = response.data.complaintList[0].complaintId;
            this.form.processRemark = response.data.complaintList[0].processRemark;
          }
          this.open = true;
          this.title = "审核";
        });
        // const complaintId = row.complaintId || this.ids
        // getComplaint(complaintId).then(response => {
        //   this.form = response.data;
        //   this.open = true;
        //   this.title = "审核";
        // });
      },
      /** 提交按钮 */
      submitForm(flag) {
        console.log(this.form.complaintId)
        this.$refs["form"].validate(valid => {
          if (valid) {
            if(flag)
              this.form.processStatus = '2';
            else
              this.form.processStatus = '-1';

            if (this.form.complaintId != null) {
              updateComplaint(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addComplaint(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const complaintIds = row.complaintId || this.ids;
        this.$confirm('是否确认删除客户投诉编号为"' + complaintIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delComplaint(complaintIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('order/complaint/export', {
          ...this.queryParams
        }, `order_complaint.xlsx`)
      }
    }
  };
</script>
